#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from subprocess import check_output, CalledProcessError

def get_pid_by_exec_name(filename):
    args = ['tasklist', '/nh', '/fi', 'IMAGENAME eq {0}'.format(filename), '/fo', 'csv']

    try:
        raw_output = check_output(args)
    except CalledProcessError as e:
        print(str(e))
        return []

    pid_list = []
    process_list = raw_output.split('\r\n'.encode("utf-8"))
    for process_info in process_list:
        tokens = process_info.split(",".encode("utf-8"))
        if len(tokens) > 1:
            pid = tokens[1].strip('"'.encode('utf-8'))
            pid_list.append(int(pid))
    return pid_list

if __name__ == '__main__':
    pid_list = get_pid_by_exec_name("chrome.exe")
    for pid in pid_list:
        print("chrome.exe: {0}".format(pid))


